#!/usr/bin/python


# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


import os
import gobject, gtk
import dbus, dbus.service, dbus.mainloop.glib 
import pynotify


## TrayNotify - Used to create a wrapper for gtk.StatusIcon
class TrayNotify:
    def __init__(self): 
        self.icon = gtk.StatusIcon()
        self.icon.set_from_file('img/burnearth.png')
        self.icon.set_tooltip('pyTrayNotify')
        if pynotify.init("TrayNotify"):
            self.notify = pynotify.Notification("Title_UNDEF", "message_UNDEF")
        else:
            print "there was a problem initializing the pynotify module"


## Create a TrayNotify object for use
status = TrayNotify()

## Function declarations ----------------------------------------------------------
def blink_signal():
    global status
    
    ## Check the blink status of the icon and reverse it
    if status.icon.get_blinking():
        status.icon.set_blinking(False)
    else:
        status.icon.set_blinking(True)  
## End def blink_signal()

def message_signal(priority,title,msg):
    global status
    ## Display 'message received' on the console
    print "Message Received"
    
    ## Pop up an alert with pynotify with our title and message
    status.notify = pynotify.Notification(title, msg)
    status.notify.attach_to_status_icon(status.icon)
    
    if priority == 0:
        status.notify.set_urgency(pynotify.URGENCY_LOW)
    elif priority == 2:
        status.notify.set_urgency(pynotify.URGENCY_CRITICAL)
    
    status.notify.show()
## End def message_signal()
## End Function declarations ------------------------------------------------------

## If this module is called as an application do the following
if __name__ == '__main__': 
    ## Create a D-BUS handle to the system bus
    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) 
    bus = dbus.SystemBus()
    
    ## Add signal receivers for blink and message_signal
    bus.add_signal_receiver( blink_signal,
                             signal_name = 'traynotify_blink',
                             dbus_interface = 'org.PyTrayNotify.Interface',
                             path = '/')
    bus.add_signal_receiver( message_signal,
                             signal_name = 'traynotify_msg',
                             dbus_interface = 'org.PyTrayNotify.Interface',
                             path = '/')
    
    ## Start a loop
    loop = gobject.MainLoop()
    loop.run()
## End if __main__

